
#include "bsp_device.h"
#include "bsp_timer.h"

/*****************************************************                       
*				bsp_init_device	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
/*****************************************************                       
*				bsp_init_device	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void bsp_init_device( p_appdevice p_Device,
					  DEVICE_RECV      p_recv,
					  DEVICE_SEND_CMOT p_create,
					  DEVICE_SEND_TMOT p_timerout,
					  DEVICE_SEND_FUC  p_send ){

	p_Device->d_recv         = p_recv;
	p_Device->d_create_timer = p_create;
	p_Device->d_deal_timerout= p_timerout;
	p_Device->d_send_fuc     = p_send;
}


/*****************************************************                       
*				bsp_set_device_recv	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void bsp_set_device_recv( p_appdevice p_Device,
						    unsigned char *p_Data,
							unsigned char  u_max,
							unsigned char  u_per ){

	bsp_init_cir_queue( &p_Device->c_RecvBuffer,
		                 p_Data,u_max,u_per );
}

/*****************************************************                       
*				bsp_set_devicerecv	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void bsp_set_device_send( p_appdevice p_Device,
						    unsigned char *p_Data,
							unsigned char  u_max,
							unsigned char  u_per ){

	bsp_init_cir_queue( &p_Device->c_SendBuffer,
		                p_Data,u_max,u_per );
}

/*****************************************************                       
*				bsp_get_device_recv
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_get_device_recv(  p_appdevice p_Device,
								   unsigned char *p_Data ,
								   unsigned char  *p_len ) {


	return bsp_pop_cir_queue_data( &p_Device->c_RecvBuffer,p_Data,p_len );
}

/*****************************************************                       
*				bsp_get_device_send
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_get_device_send( p_appdevice p_Device, 
								  unsigned char *p_Data ,
								  unsigned char *p_len ) {

	return bsp_pop_cir_queue_data( &p_Device->c_SendBuffer,p_Data,p_len );

}


/*****************************************************                       
*				bsp_set_device_recv_event	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void bsp_set_device_recv_event( p_appdevice p_Device, OS_EVENT *_event ){

	p_Device->p_OS_RecvEvent = _event;

}


/*****************************************************                       
*				bsp_set_device_recv_event	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void bsp_set_device_send_event( p_appdevice p_Device, OS_EVENT *_event ){

	p_Device->p_OS_SendEvent = _event;
}

/*****************************************************                       
*				bsp_trigger_device_recv
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_trigger_device_recv(p_appdevice p_Device,  
								 unsigned char *p_Data, 
								 unsigned char u_len ){
	
	uint16_t u_Ret = 0;

	u_Ret = bsp_push_cir_queue_data_tail( &p_Device->c_RecvBuffer,p_Data,u_len );
#if BSP_OS_EN == 1

	if( u_Ret ){

		OSQPost( p_Device->p_OS_RecvEvent,p_Device );
	}
#endif

	return u_Ret;
}


/*****************************************************                       
*				bsp_trigger_device_send
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_trigger_device_send( p_appdevice p_Device, 
								 unsigned char *p_Data, 
								 unsigned char u_len ){
	uint16_t u_Ret = 0;
	
	u_Ret = bsp_push_cir_queue_data_tail( &p_Device->c_SendBuffer,p_Data,u_len );
#if BSP_OS_EN == 1
	
	if( u_Ret ){
		
		OSQPost( p_Device->p_OS_SendEvent,p_Device );
	}
#endif
	
	return u_Ret;
}


/*****************************************************                       
*				bsp_RecvHandle
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_RecvHandle( p_appdevice p_Device ){

	unsigned char recvData[DEVICE_RECV_MAX];

	bsp_timer t_timer = { NULL};

	unsigned char      u_RecvLen;
	if( p_Device == NULL ){

		return FALSE;
	}
	
	if( bsp_pop_cir_queue_data( &p_Device->c_RecvBuffer, recvData,&u_RecvLen) ){
			
	
		if( p_Device->d_recv != NULL ){
			
			if( p_Device->d_recv( p_Device, recvData, u_RecvLen ) ){

				p_Device->d_create_timer( &t_timer, recvData, u_RecvLen );
				
				bsp_findDeltimer( &t_timer  );

			}
		}
	}
    
    return TRUE;
	
}

/*****************************************************                       
*				bsp_SendHandle
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t bsp_SendHandle( p_appdevice p_Device ){
	
	unsigned char sendData[DEVICE_RECV_MAX];
	

	bsp_timer   t_timer = { NULL };
	
	unsigned char      u_SendLen;
	if( p_Device == NULL ){
		
		return FALSE;
	}
	
	if( bsp_pop_cir_queue_data( &p_Device->c_SendBuffer, sendData,&u_SendLen) ){
		
		
		if( p_Device->d_send_fuc != NULL ){

			p_Device->d_send_fuc( sendData, u_SendLen );


			if( p_Device->d_create_timer != NULL ){

				if( p_Device->d_create_timer( &t_timer,sendData,u_SendLen )){
					
					bsp_get_timer( e_tr_one, t_timer.tr_data, 
								   t_timer.u_timer_max,p_Device->d_deal_timerout );
				}
			}
		}
	}
	return TRUE;
}